package com.se.net;

import com.util.BeanUtils;
import io.netty.util.internal.PlatformDependent;
import org.drasyl.channel.tun.TunAddress;
import org.drasyl.channel.tun.TunChannel;
import org.drasyl.channel.tun.jna.TunDevice;
import org.drasyl.channel.tun.jna.darwin.DarwinTunDevice;
import org.drasyl.channel.tun.jna.linux.LinuxTunDevice;
import org.drasyl.channel.tun.jna.windows.WindowsTunDevice;

import java.net.SocketAddress;

/**
 * @author hdu_huang
 * @since 2024/3/6 10:02
 */
public class TunProChannel extends TunChannel {

    @Override
    protected void doBind(final SocketAddress localAddress) throws Exception {

        if (PlatformDependent.isWindows()) {

            TunDevice tunDevice = NetUtil.openTunDevice(((TunAddress) localAddress).ifName());
            BeanUtils.setDeclaredFieldValue(TunChannel.class, "device", this, tunDevice);

        } else {
            super.doBind(localAddress);
        }
    }
}
